Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | 'use client';
import { useState, useEffect } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import useLoadNamespace from '@/hooks/useLoadNamespace';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Label } from '@/components/ui/label';
import { Alert, AlertDescription } from '@/components/ui/alert';
import {
Folder,
FolderOpen,
ArrowUp,
RefreshCw,
Loader2,
CheckCircle,
AlertCircle,
Home,
HardDrive
} from 'lucide-react';
import { mediaScannerService, type FolderItem } from '@/services/mediaScanner';
interface FolderExplorerProps {
selectedPath: string;
onPathSelect: (path: string) => void;
className?: string;
}
export default function FolderExplorer({ selectedPath, onPathSelect, className }: FolderExplorerProps) {
useLoadNamespace('admin/mediaScanner');
const { t } = useTranslation('admin/mediaScanner');
const [currentPath, setCurrentPath] = useState<string>('/');
// Fetch folders for current path
const {
data: folders,
isLoading: loadingFolders,
error: foldersError,
refetch: refetchFolders
} = useQuery({
queryKey: ['folders', currentPath],
queryFn: () => mediaScannerService.listFolders(currentPath),
staleTime: 30000, // Cache for 30 seconds
});
// Update current path when selected path changes externally
useEffect(() => {
if (selectedPath && selectedPath !== currentPath) {
setCurrentPath(selectedPath);
}
}, [selectedPath]);
const handleFolderSelect = (folder: FolderItem) => {
if (folder.isDirectory) {
setCurrentPath(folder.path);
}
};
const handleSelectCurrentFolder = () => {
onPathSelect(currentPath);
};
const handleGoUp = () => {
const pathParts = currentPath.split('/').filter(Boolean);
if (pathParts.length > 0) {
pathParts.pop();
const parentPath = '/' + pathParts.join('/');
setCurrentPath(parentPath === '/' ? '/' : parentPath);
}
};
const handleGoToRoot = () => {
setCurrentPath('/');
};
const formatFileSize = (bytes?: number): string => {
if (!bytes) return '';
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`;
};
const formatLastModified = (dateString?: string): string => {
if (!dateString) return '';
try {
const normalized = dateString.includes('T')
? dateString
: dateString.replace(' ', 'T');
const date = new Date(normalized);
if (Number.isNaN(date.getTime())) {
return '';
}
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
} catch {
return '';
}
};
const pathParts = currentPath.split('/').filter(Boolean);
return (
<Card className={className}>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Folder className="h-5 w-5" />
{t('mediaScanner.folderExplorer.title')}
</CardTitle>
<CardDescription>
{t('mediaScanner.folderExplorer.description')}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{/* Navigation Bar */}
<div className="flex items-center gap-2 flex-wrap">
<Button
variant="outline"
size="sm"
onClick={handleGoToRoot}
disabled={currentPath === '/'}
>
<Home className="h-4 w-4" />
</Button>
<Button
variant="outline"
size="sm"
onClick={handleGoUp}
disabled={currentPath === '/'}
>
<ArrowUp className="h-4 w-4" />
{t('mediaScanner.folderExplorer.goUp')}
</Button>
<Button
variant="outline"
size="sm"
onClick={() => refetchFolders()}
disabled={loadingFolders}
>
<RefreshCw className={`h-4 w-4 ${loadingFolders ? 'animate-spin' : ''}`} />
{t('mediaScanner.folderExplorer.refresh')}
</Button>
</div>
{/* Breadcrumb Path */}
<div className="flex items-center gap-1 text-sm">
<Label>{t('mediaScanner.folderExplorer.currentPath')}:</Label>
<div className="flex items-center gap-1 font-mono bg-muted px-2 py-1 rounded">
<HardDrive className="h-3 w-3" />
<span>/</span>
{pathParts.map((part, index) => (
<span key={index} className="flex items-center gap-1">
<span
className="cursor-pointer hover:text-primary"
onClick={() => {
const newPath = '/' + pathParts.slice(0, index + 1).join('/');
setCurrentPath(newPath);
}}
>
{part}
</span>
{index < pathParts.length - 1 && <span>/</span>}
</span>
))}
</div>
</div>
{/* Folder List */}
<div className="border rounded-lg">
<div className="h-64 overflow-y-auto">
{loadingFolders ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="h-6 w-6 animate-spin" />
<span className="ml-2">{t('mediaScanner.folderExplorer.loading')}</span>
</div>
) : foldersError ? (
<div className="p-4">
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertDescription>
{t('mediaScanner.folderExplorer.error')}
</AlertDescription>
</Alert>
</div>
) : folders?.success && folders.data.length > 0 ? (
<div className="p-2">
{folders.data
.filter(f => f.isDirectory)
.sort((a, b) => a.name.localeCompare(b.name))
.map((folder) => (
<div
key={folder.path}
className="flex items-center gap-3 p-2 rounded hover:bg-muted cursor-pointer group"
onClick={() => handleFolderSelect(folder)}
>
<FolderOpen className="h-4 w-4 text-blue-500 flex-shrink-0" />
<div className="flex-1 min-w-0">
<div className="font-medium truncate">{folder.name}</div>
{folder.lastModified && (
<div className="text-xs text-muted-foreground">
{formatLastModified(folder.lastModified)}
</div>
)}
</div>
{folder.size && (
<Badge variant="outline" className="text-xs">
{formatFileSize(folder.size)}
</Badge>
)}
</div>
))}
</div>
) : (
<div className="flex items-center justify-center py-8 text-muted-foreground">
<Folder className="h-8 w-8 mb-2" />
<div className="text-center">
<div>{t('mediaScanner.folderExplorer.noFolders')}</div>
</div>
</div>
)}
</div>
</div>
{/* Select Current Folder */}
<div className="space-y-2">
<Button
onClick={handleSelectCurrentFolder}
disabled={currentPath === '/'}
className="w-full"
variant={selectedPath === currentPath ? "default" : "outline"}
>
<CheckCircle className="h-4 w-4 mr-2" />
{t('mediaScanner.folderExplorer.selectFolder')}: {currentPath}
</Button>
{selectedPath && selectedPath !== '/' && selectedPath !== currentPath && (
<Alert>
<CheckCircle className="h-4 w-4" />
<AlertDescription>
{t('mediaScanner.folderExplorer.selectedFolder')} <code className="font-mono">{selectedPath}</code>
</AlertDescription>
</Alert>
)}
</div>
{/* Folder Stats */}
{folders?.success && folders.data.length > 0 && (
<div className="text-xs text-muted-foreground text-center">
{folders.data.filter(f => f.isDirectory).length} folders, {folders.data.filter(f => !f.isDirectory).length} files
</div>
)}
</CardContent>
</Card>
);
}
|